package com.share.thread;

/**
 * @desc:
 * @author:caifan
 * @date:2022/1/21
 */
public class SumTask {
    public static void main(String[] args) throws Exception {
        Sum sum = new Sum();
        for (int i = 1; i <= 10; i++) {
            int finalI = i;
            Thread thread = new Thread(() -> sum.sum(finalI));
            thread.start();
            thread.join();
            System.out.println(sum.total);
        }
    }
}

class Sum {
    protected volatile int total = 0;
    public synchronized int sum(int i) {
        int sum = 0;
        int start = (i - 1) * 10 + 1;
        int end = i * 10;
        for (;start <= end; start++) {
            sum += start;
        }
        total += sum;
        System.out.println(sum);
        return sum;
    }

}
